//会话表的操作

#pragma once
#include <string>
#include <memory> // std::auto_ptr
#include <cstdlib> // std::exit
#include <iostream>
#include <odb/database.hxx>
#include <odb/mysql/database.hxx>
#include "logger.hpp"
#include "relation.hxx"
#include "relation-odb.hxx"
#include "mysql.hpp"

namespace zrb
{
    class RelationTable
    {
    public:  
        using ptr = std::shared_ptr<RelationTable>;
        RelationTable(const std::shared_ptr<odb::core::database>& db)
            :_db(db)
        {}

        //新增关系信息
        bool insert(const std::string& uid,const std::string& pid)
        {
            try 
            {
                //3.获取事务对象开启事务
                Relation r1(uid,pid); //{1,2} {2.1}
                Relation r2(pid,uid);
                odb::transaction trans(_db->begin());
                _db->persist(r1);
                _db->persist(r2);
                //4.提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("新增{}-{}数据出错：{}",uid,pid,e.what());
                return false;
            }
            return true;
        }

        //移除关系信息
        bool remove(const std::string& uid,const std::string& pid)
        {
            try 
            {
                //3.获取事务对象开启事务
                odb::transaction trans(_db->begin());
                typedef odb::query<Relation> query;
                typedef odb::result<Relation> result;
                _db->erase_query<Relation>(query::user_id == uid && query::peer_id == pid); //删除条件
                _db->erase_query<Relation>(query::user_id == pid && query::peer_id == uid); //删除条件
                //4.提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("删除好友关系数据出错：{}-{} {}",uid,pid,e.what());
                return false;
            }
            return true;
        }

        //判断关系是否存在
        bool exist(const std::string& uid,const std::string& pid)
        {   
            bool flags = false;
            try {
                //获取事务对象开启事务
                odb::transaction trans(_db->begin());
                typedef odb::query<Relation> query;
                typedef odb::result<Relation> result;

                //查询
                result r(_db->query<Relation>(query::user_id == uid && query::peer_id == pid));
                
                flags = !r.empty();

                //5. 提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("获取好友关系表失败：{}-{} {}",uid,pid,e.what());
            }
            return flags;
        }

        //获取指定用户的好友ID
        std::vector<std::string> friends(const std::string& uid)
        {
            std::vector<std::string> res;
            try {
                //获取事务对象开启事务
                odb::transaction trans(_db->begin());
                typedef odb::query<Relation> query;
                typedef odb::result<Relation> result;

                //查询
                result r(_db->query<Relation>(query::user_id == uid));

                //遍历
                for (auto it = r.begin(); it != r.end(); ++it) 
                {
                    res.push_back(it->peer_id());
                }
                //5. 提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("通过用户id列表查询好友ID出错：{}",e.what());
            }
            return res;
        }

    private:
        std::shared_ptr<odb::core::database> _db;
    };
}



